package com.lian.hope.mapper;

import com.lian.hope.model.po.Dynamic;
import com.lian.hope.model.po.DynamicComment;
import com.lian.hope.model.vo.DynamicCommentVO;
import com.lian.hope.model.vo.DynamicVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface DynamicMapper {
    @Select(
        "insert into dynamic(user_id,title,content,img_url,create_time,user_avatar,nickname) values" +
        "(#{userId}," +
        "#{title}," +
        "#{content},#{imgUrl},#{createTime},#{userAvatar},#{nickname})")
    void insertDynamic(Dynamic dynamic);

    @Select("select * from dynamic")
    List<DynamicVO> listDynamic();

    @Select("select * from dynamic where user_id = #{userId} ORDER BY create_time DESC")
    List<DynamicVO> listDynamicByUserId(Long userId);

    @Select("delete from dynamic where id =#{dynamicId}")
    void deleteDynamicById(Long dynamicId);

    @Select("insert into dynamic_likes (dynamic_id,user_id,like_time) values(#{dynamicId},#{userId},#{likeTime})")
    void insertDynamicLike(Long dynamicId, Long userId, LocalDateTime likeTime);

    @Select("delete from dynamic_likes where dynamic_id = #{dynamicId} and user_id = #{userId}")
    void deleteDynamicLikeById(Long dynamicId, Long userId);

    @Select("update dynamic set like_count = like_count + 1 where id = #{dynamicId}")
    void incrementDynamicLikeCount(Long dynamicId);

    @Select("update dynamic set like_count = like_count - 1 where id = #{dynamicId}")
    void decrementDynamicLikeCount(Long dynamicId);

    @Select("select id from dynamic_likes where dynamic_id = #{dynamicId} and user_id = #{userId}")
    Long queryDynamicLike(Long dynamicId, Long userId);

    @Select(
        "insert into dynamic_comment(dynamic_id,user_id,content,create_time,user_nickname,user_avatar_url) values" +
        "(#{dynamicId},#{userId}," + "#{content},#{createTime},#{userNickname},#{userAvatarUrl})")
    void insertDynamicComment(DynamicComment dynamicComment);

    @Select("select * from dynamic_comment where dynamic_id = #{dynamicId}")
    List<DynamicCommentVO> listDynamicCommentById(Integer pageNum, Integer pageSize, Long dynamicId);

    @Select("select * from dynamic where id = #{dynamicId}")
    DynamicVO getDynamicById(Long dynamicId);

    @Select("select id from dynamic_likes where dynamic_id = #{dynamicId} and user_id = #{userId}")
    Long checkUserLikeDynamic(Long dynamicId, Long userId);
}
